package interfazGrafica;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;

import negocios.Minibook;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AgregarAmistad extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField txMail1;
	private JTextField txMail2;
	private Minibook book;
	private Pattern pat = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AgregarAmistad dialog = new AgregarAmistad();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AgregarAmistad() {
		setTitle("Agregar Amistad");
		setBounds(100, 100, 490, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		txMail1 = new JTextField();
		txMail1.setBounds(90, 104, 312, 19);
		contentPanel.add(txMail1);
		txMail1.setColumns(10);
		
		txMail2 = new JTextField();
		txMail2.setColumns(10);
		txMail2.setBounds(90, 131, 312, 19);
		contentPanel.add(txMail2);
		
		JLabel lblMail1 = new JLabel("Mail 1:");
		lblMail1.setBounds(12, 106, 70, 15);
		contentPanel.add(lblMail1);
		
		JLabel lblMail2 = new JLabel("Mail 2:");
		lblMail2.setBounds(12, 133, 70, 15);
		contentPanel.add(lblMail2);
		
		JLabel lblEstablecer = new JLabel("Establecer una relacion de Amistad entre los siguientes e-mails:");
		lblEstablecer.setBounds(12, 58, 460, 15);
		contentPanel.add(lblEstablecer);
		
		JLabel lblNuevaAmistad = new JLabel("Nueva Amistad");
		lblNuevaAmistad.setFont(new Font("Dialog", Font.BOLD, 20));
		lblNuevaAmistad.setForeground(Color.BLUE);
		lblNuevaAmistad.setBounds(148, 12, 172, 19);
		contentPanel.add(lblNuevaAmistad);
		
		JButton btnRegistrarAmistad = new JButton("Registrar Amistad");
		btnRegistrarAmistad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String mail1= txMail1.getText();
				String mail2= txMail2.getText();
				Matcher mat = pat.matcher(mail1);
				Matcher mat2 = pat.matcher(mail2);
				book= Principal.getMinibook();
				
				if(mat.find() && mat2.find()){
					if (book.estaRegistrado(mail1) && book.estaRegistrado(mail2)){	
						if (!book.sonAmigos(mail1, mail2)){
							
							book.agregarAmistad(mail1, mail2);
							JOptionPane.showMessageDialog(contentPanel,"La relacion de amistad fue creada");
							setVisible(false);
						}
						else{
							JOptionPane.showMessageDialog(contentPanel,"La relacion de amistad ya existe");
							setVisible(false);
						}
					}
					else {
						JOptionPane.showMessageDialog(contentPanel,"Uno o ambos Emails no se encuentran registrados en el Minibook");
						setVisible(false);
					}
				}else{
					JOptionPane.showMessageDialog(contentPanel, "Ingrese emails validos");
				}
				Principal.refresh();
			}
		});
		btnRegistrarAmistad.setBounds(64, 236, 180, 25);
		contentPanel.add(btnRegistrarAmistad);
		
		JButton btnSalir = new JButton("Salir");
		btnSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				Principal.hacerVisible(true);			
				}
		});
		btnSalir.setBounds(248, 236, 180, 25);
		contentPanel.add(btnSalir);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		}
		
	}
}
